//
//  LTMainController.swift
//  微博08
//
//  Created by 123 on 16/2/29.
//  Copyright © 2016年 Tsr. All rights reserved.
//

import UIKit

class LTMainController: UITabBarController, LTMainTabBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 只使用于tabBar图片和文字的颜色一样
        // tabBar.tintColor = UIColor.orangeColor()
        
        // tabBar是只读,使用KVC来赋值
        let tabBar = LTMainTabBar()
        // 4.设置代理
        tabBar.composeDelegate = self
        setValue(tabBar, forKey: "tabBar")
        
        addchildviewcontrollers()
    }
    
    // MARK: 实现代理方法,响应点击事件
    // 5.实现代理方法
    func mainTabBarComposeClick() {
        print("LTMainController被点击了")
    }
    
    func addchildviewcontrollers() {
        // 首页
        let homeVC = LTHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        // 消息
        let messageVC = LTMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 占位控制器
        addChildViewController(UIViewController())
        
        // 发现
        let discoverVC = LTDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        
        // 我
        let meVC = LTMeViewController()
        addChildViewController(meVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    /// 添加tabBar的子控制器
    func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        // 设置title
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        // 设置选中图片
        let highedImageName = imageName + "_highlighted"
        // 设置选中不要渲染
        controller.tabBarItem.selectedImage = UIImage(named: highedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置选中文字的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        // 添加为tabBar的子控制器,并且包装导航控制器
        addChildViewController(UINavigationController(rootViewController: controller))
    }
}
